package com.boot.contorller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.boot.config.OrderProperties;
import com.boot.order.pojo.Order;
import com.boot.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("order")
//@RefreshScope
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderProperties orderProperties;

    //创建订单
    @GetMapping("create")
    public Order create(@RequestParam("productId") Long productId,
                        @RequestParam("userId") Long userId) {
        return orderService.createOrder(productId, userId);
    }


    @GetMapping("deskill")
    @SentinelResource(value = "deskill-order", fallback = "deskillFallBack")
    public Order deskill(@RequestParam(value = "productId", required = false) Long productId,
                        @RequestParam(value = "userId", required = false) Long userId) {
        Long pid = productId == null ? 1L : productId;
        return orderService.createOrder(pid, userId);
    }

    @GetMapping("config")
    public String config() {
        return orderProperties.getTimeout() + "  " + orderProperties.getAutoConfirm() + "   " + orderProperties.getDbUrl()
                + "  " + orderProperties.getName();
    }

    @GetMapping("read")
    public String read() {
        return "read success";
    }

    @GetMapping("write")
    public String write() {
        return "read write";
    }


    public Order deskillFallBack(Long productId, Long userId,Throwable e) {
        Order order = new Order();
        order.setId(777777777777777L);
        order.setAddress("秒杀异常：" + e.getMessage());
        return order;
    }
}


